by Kenn Scribner
In This Chapter
Many aspects of application development set the truly professional job apart from the rest. One aspect that is surprisingly easy to exploit is the addition of colorful, fast graphics, sound, auxiliary input devices, and such. In this chapter, youll explore the basics of Microsofts multimedia extension library, DirectX, and youll examine in particular DirectDraw, which targets graphics. Lets begin with a brief history lesson.
Not so many years ago, Microsoft released the Game SDK in response to claims that Windows didnt offer game developers a viable platform for high-performance game execution. In fact, game developers had a pointWindows wasnt a good platform for high frame rate animation. Windows was (and is) a wonderful platform for software development because it provides you, the developer, with a platform-independent architecture. By and large, you are unconcerned with the specifics of any individual system. You program to the idealized Windows model, and Windows will handle the specific system details for you.
But platform independence always comes at a cost, and in this case (to the game developers, anyway), it was throttled animation frame rates. That is, the Windows GDI simply couldnt update the screen as fast as game developers required. Their games, therefore, were slow and sluggish, and Windows was pretty much abandoned as a game platform. Developers instead turned to DOS as their architecture of choice for the hottest games of the day (remember Doom?)
Of course, this was at a time in history when Microsoft was trying to wean the computer market off DOS. They had to invent some mechanism for high-performance animation (and later input and sound) if they wanted the game community to abandon DOS in favor of Windows. The Game SDK was Microsofts initial answer. Today, the Game SDK has grown into the DirectX family of developer APIs. I wont be teaching game programming in this chapter. Instead, Ill concentrate on explaining DirectX technology. Ill specifically describe techniques to use DirectDraw (a major piece of DirectX) from within your own applications to enhance your applications capabilities and feature sets.
Tip:Dont believe for a moment that DirectX is only suited for game programming. Because of its performance-minded approach, it is an ideal technological choice for adding multimedia features to any application.
DirectX is a COM-based system that transcends both driver-level and application-level software layers. The main DirectX design goal was to be fast while still offering a measure of device independence. Microsoft achieved this by implementing the architecture you see in Figure 30.1. Here, you have the choice of using the full-flavored Windows API, or you can take the leaner and meaner DirectX path.
Figure 30.1 The DirectX architecture.
If you look specifically at the DirectX portion of Figure 30.1, you see two main components: the HAL and the HEL. HAL, or Hardware Abstraction Layer (a term no doubt borrowed from Windows NT) is responsible for determining the capabilities of the local hardware and offering them to you in a device-independent fashion. For those capabilities DirectX offers but that arent supported by the local hardware configuration, the HEL, or Hardware Emulation Layer, takes over. On any given system, DirectX uses a blend of HAL and HEL functionality, though you do have the option of forcing DirectX to use only HEL functions. This would present your application with a worst-case performance scenario, which is well suited for development. If your application performs well with little to no special hardware support, it will perform well no matter what the configuration in the field.
DirectX provides you with several components optimized to work with their respective subsystem. The first DirectX component, and probably the most well-known, is DirectDraw. DirectDraw provides you with the mechanisms you require to bypass the Windows GDI in favor of faster bitmap data transfers and the resulting higher animation frame rates. But drawing functions alone werent enough for the gaming community, so Microsoft developed DirectSound, DirectInput, DirectPlay, and DirectSetup. There is also another component added more recently than the restDirect3D.
DirectSound enables you to transcend the Win32 PlaySound() API call by giving you access to hardware-based (or -emulated) sound mixing capability with the added feature of 3D sound emulation. Sound mixing enables you to generate a single sound stream from many simultaneous (stored) sound inputs. This clearly has benefit to the game development community, as games frequently require multiple sound tracks. The added benefit of directional sound is especially intriguing.
DirectInput is used to gather input device feedback, such as when using a keyboard, a joystick, or some other tracking device. And starting with DirectX Version 5, you not only receive inputs from a device, but you can also send outputs to the device to simulate force applied against the input device. This is known as force feedback. This would enable you to run a flight simulation on your computer and feel the yoke push back during simulated high-G maneuvers. Many game developers find this feature exciting, as it adds a tremendous amount of realism to their simulations and games.
DirectPlay adds the multiuser perspective to your application. If youve ever seen online games, you know the game participants all want to annihilate each other, and its much more satisfying to them when they blow away a live players character versus a computer-generated one. But gaming issues aside, imagine the possibilities for your application if you enable your users to connect to each other and collaborate. DirectPlay makes this quite easy to implement.
DirectSetup is the mechanism you use to install DirectX on systems that dont currently have DirectX capability. This currently includes Windows 95 installations only, however. Windows 98 is shipped with DirectX (upgrades may be possible when available, though) and Windows NT computers require DirectX to be installed through NT Service Packs only (security being a primary concern here).
Direct3D is built upon DirectDraw, so it uses many of the primitives DirectDraw provides you. It offers an alternative to other 3D programming APIs, including OpenGL. Inside Direct3D, youll find the basic 3D programming support as well as some high-end 3D technology you dont commonly see in other PC-based APIs (high-end texture mapping, for example).
Given this high-level tour of DirectX, its time to see how you add this compelling technology to your applications. Ill start with some basic concepts you need to begin working with DirectX. Then, Ill discuss DirectDraw in detail, which is the oldest and largest of the DirectX components.
The fundamental architecture that binds the DirectX family of components together is COM, and though you dont need to be a COM wizard to use DirectX from within your applications, you need to understand how COM works (if only at a high level) and be familiar with COM-specific coding practices. There are also some terms and concepts I should introduce. Ill start with the necessary COM information that varies from Chapter 10, COM.